package com.cherrish.demo;

/**
 * @author oxchains
 * @time 2019-03-01 16:53
 * @name IpInt
 * @desc:
 */
public class IpInt {

    public static void main(String[] args) {
        String ip = "192.168.1.110";
        int ip1 = ipToInteger(ip);
        System.out.println(ip1);
        String ip2 = integerToIp(ip1);
        System.out.println(ip2);
    }

    public static int ipToInteger(String ip){
        String[] ips = ip.split("\\.");
        int ipFour = 0;
        //因为每个位置最大255，刚好在2进制里表示8位
        for(String ip4: ips){
            Integer ip4a = Integer.parseInt(ip4);
            //这里应该用+也可以,但是位运算更快
            ipFour = (ipFour << 8) | ip4a;
        }
        return ipFour;
    }

    public static String integerToIp(Integer ip){
        //思路很简单，每8位拿一次，就是对应位的IP
        StringBuilder sb = new StringBuilder();
        for(int i = 3; i >= 0; i--){
            int ipa = (ip >> (8 * i)) & (0xff);
            sb.append(ipa + ".");
        }
        sb.delete(sb.length() - 1, sb.length());
        return sb.toString();
    }
}
